// 封装成函数
export function hyRequest(options) {
  return new Promise((resolve, reject) => {
    wx.request({
      ...options,
      success: (res) => {
        resolve(res.data);
      },
      fail: reject,
    });
  });
}

// 封装成类 -> 实例
class HYRequest {
  constructor(baseURL) {
    this.baseURL = baseURL;
  }

  request(options) {
    return new Promise((resolve, reject) => {
      wx.request({
        ...options,
        url: this.baseURL + options.url,
        success: (res) => {
          resolve(res.data);
        },
        fail: (error) => {
          console.log("error", error);
        },
      });
    });
  }

  get(options) {
    return this.request({ ...options, method: "get" });
  }

  post(options) {
    return this.request({ ...options, method: "post" });
  }
}

export const hyReqInstance = new HYRequest("http://123.207.32.32:1888/api");
export const hyLoginReqInstance = new HYRequest("http://123.207.32.32:3000");
